<?php 
/**********************************INCLUDE*********************************** *
* **************************************************************************** */
include_once '../app/App.php';
include_once '../account/Account.php';
include_once '../db/DatabaseManager.php';

/**
 * @author nicholas malacarne <nicholas.malacarne@gmail.com>
 * 
 * This script is used to log an account into the app.
 */

// required post data fields -nm
$required = array( 'username', 'password' );

try {
    
    // check if we have required parameters -nm
    if ( ! App::paramOK( $required, $_GET ) ){
        
        throw new ErrorException( "Error Processing Parameters !" );
    }
    
    // get instance of App -nm
    $app = App::init();
    
    // create db manager obj -nm
    //$db = new DatabaseManager( 'localhost', 'root', 'admin', 'gtcaitp' );
    
    // TODO: query database -nm
    //$data = $db->getRowByValue( 'accounts', 'email', $email );
    
    // create temporary 'db' array for testing -nm
    $db = array( "username" => "admin", "password" => "4c01c266b6fcb214f461ea4ed4375d28" );
    
    // strip post data -nm
    $username  = filter_input( INPUT_GET, 'username' );
    $password  = filter_input( INPUT_GET, 'password' );
    
    // double check to make sure username/password values meet min length -nm
    if ( strlen( $username ) >= 5 && strlen( $password ) >= 5 ){
        
        if ( $username == $db[ "username" ] && $password == $db[ "password" ] ){
            
            // create temporary account for testing -nm
            $app->setAccount( new Account( array( "username" => $username ) ) );
        }
        
    }else{
        
        throw new ErrorException( "Username/Password too short !" );
    }
    
    // TODO: create AJAX response class -nm
    echo json_encode(Array());
    
}catch( Exception $e ){
    
    $report[ 'exception' ] = array();
    
    $report[ 'exception' ][ 'file' ] = $e->getFile();
    $report[ 'exception' ][ 'line' ] = $e->getLine();
    $report[ 'exception' ][ 'msg' ] = $e->getMessage();
    
    echo json_encode( $report );
    
} // TODO: handle specific exception -nm

exit();
